package com.example.demo.Service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.auth0.jwt.algorithms.Algorithm;
import com.example.demo.Algorithm.AlgorithmCollection;
import com.example.demo.Entity.Employee;
import com.example.demo.Entity.Experience;
import com.example.demo.Entity.HumanResource;
import com.example.demo.Entity.User;
import com.example.demo.GeneralResponse.APIResponse;
import com.example.demo.Repository.IEmployeeRepository;
import com.example.demo.Repository.IExperienceRepository;
import com.example.demo.Repository.IHRRepository;
import com.example.demo.Repository.IPerformanceRepository;
import com.example.demo.Repository.IUserRepository;

/**
 * @version: V0.3
 * @author: 郑皓杰 吴嘉鑫
 * @className: EmployeeService
 * @packageName: com.example.demo.Service
 * @description: 这是实现员工业务逻辑的服务类
 * @data: 2021-07-14
 **/

@Service
public class EmployeeService {
	@Autowired
	private IEmployeeRepository empRepository;
	@Autowired
	private IUserRepository userRepository;
	@Autowired 
	private IHRRepository hrRepository;
	@Autowired
	private IExperienceRepository expRepository;
	@Autowired
	private IPerformanceRepository perfRepository;
	@Autowired
	private AlgorithmCollection alo;
	
	/**
	* @author:郑皓杰
	* @methodsName: register
	* @description: employee在数据库中注册存储
	* @param:  String emname ,String telephone, String empassword
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> register(String username ,String telephone, String empassword) {
		//if(empRepository.findBytelephone(telephone) != null)  return "repeatedtelephone";
		//User suser = userRepository.findByUsername(username);
		//if(suser != null)  return "repeatedusername";
		if(userRepository.findByUsername(username) != null) return APIResponse.fail("repeatedusername");
		if(empRepository.findBytelephone(telephone) != null)return APIResponse.fail("repeatedtelephone");
		Employee emp = new Employee();
		User user = new User();
		user.setUsername(username);
		user.setPassword(empassword);
		userRepository.save(user);
		emp.setUsername(username);
		emp.setTelephone(telephone);
		emp.setFree(true);
		empRepository.save(emp);
		
		return APIResponse.success("success",null);		
	}
	
	
	/**
	* @author:吴嘉鑫 郑皓杰
	* @methodsName: setInfo
	* @description: employee更改在数据库存储的信息
	* @param:  Integer index, String emname,String gender,Date birth,String email,String telephone
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> setInfo(Integer Empid, String realname,String gender,Date birth,String email,String telephone,Integer expectedsalary) {	
		Employee emp = empRepository.getById(Empid);
		if(!(empRepository.findBytelephone(telephone) == emp||empRepository.findBytelephone(telephone)==null)) return APIResponse.fail("repeatedtelephone");
		if(!(empRepository.findByemail(email) == emp||empRepository.findByemail(email)==null)) return APIResponse.fail("repeatedemail");

		emp.setRealname(realname);
		emp.setGender(gender);
		emp.setBirth(birth);
		emp.setEmail(email);
		emp.setTelephone(telephone);
		emp.setExpectedsalary(expectedsalary);
		empRepository.save(emp);
		return APIResponse.success("changesuccess",null);
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: getInfo
	* @description: employee获取自己信息
	* @param:  String emname
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getInfo(String username) {	
		return APIResponse.success("successget",empRepository.findByUsername(username));
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: login
	* @description: employee进行登录验证
	* @param:  String emname,String password
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> login(String username,String password) {
		Employee emp = empRepository.findByUsername(username);
		User user = userRepository.findByUsername(username);
		String spassword = user.getPassword();

		if(emp==null||!spassword.equals(password)) return APIResponse.fail("wrongusernameorpassword");
		return APIResponse.success("successlogin",null);
	}

	/**
	* @author:郑皓杰
	* @methodsName: searchFree
	* @description: 查询招人公司
	* @param:  
	* @return: List<HumanResource>
	* @throws: 
	*/
	public List<HumanResource> searchFree() {
		return hrRepository.fingByState(true);
	}
	
	
	/**
	* @author:郑皓杰
	* @methodsName: getInfo
	* @description: employee获取自己经历 
	* @param:  String username
	* @return: APIResponse
	* @throws: 
	*/
	public List<Experience> getExp(String username) {	
		return expRepository.findByEmpid(empRepository.findByUsername(username).getEmpid());
	}
	
	/**
	* @author:郑皓杰
	* @methodsName: getInfo
	* @description: employee获取当前所在公司绩效
	* @param:  Integer empid
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getNowPerformance(Integer empid) {	
		if(empRepository.findByEmpid(empid).isFree()) return APIResponse.fail("no job");
		return APIResponse.success("success get",perfRepository.findByExpid(expRepository.findExpidByEmpidAndStillOnWork(empid)));
	}
	
	/**
	* @author:吴嘉鑫
	* @methodsName: getPastSalary
	* @description: employee获取自己以往的工资，并分析
	* @param:  Integer empid
	* @return: APIResponse
	* @throws: 
	*/
	public APIResponse<?> getPastSalary(Integer empid){
		List<Integer> list = expRepository.findsalaryByempid(empid);
		Employee emp = empRepository.findByEmpid(empid);
		String overrated = null;
		String FrequentSuspended = null;
		if(alo.isOverrated(emp)) overrated = "的需求工资常常高于实际工资";
		if(alo.isFrequentSuspended(emp)) FrequentSuspended="经常跟换职位";
		return APIResponse.success("根据分析，您"+overrated+FrequentSuspended,list);
	}
	
	
	
	
	
}